package bit.springblogdemo.common.advice;
import bit.springblogdemo.common.exception.BlogException;
import bit.springblogdemo.pojo.response.Result;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.HandlerInterceptor;

@ResponseBody
@Slf4j
@ControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler
      public Result exceptionHandler(Exception ex){
      log.error("发生异常,e:",ex);
      return Result.fail(ex.getMessage());
  }

  @ExceptionHandler
    public Result exceptionHandler(BlogException ex){
        log.error("发生异常,e{}:",ex);
        return Result.fail(ex.getErrMsg());
    }

    @ExceptionHandler
    public Result exceptionHandler(IllegalArgumentException ex){
        log.error("发生异常,e:{}",ex.getMessage());
        return Result.fail("参数校验失败");
    }

    @ExceptionHandler
    public Result exceptionHandler(MethodArgumentNotValidException ex){
        String msg=ex.getBindingResult().getFieldError().getDefaultMessage();
        log.error("发生异常,e:{}",ex.getMessage());
        return Result.fail(msg);
    }

}
